package other;

public class LeetCode_2 {
    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public static void main(String[] args) {
        LeetCode_2 leet = new LeetCode_2();
        ListNode l1 = new ListNode(0, new ListNode(0, new ListNode(1,null)));
        ListNode l2 = new ListNode(0, new ListNode(0, new ListNode(1,null)));
        ListNode listNode = leet.addTwoNumbers(l1, l2);
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int jinwei = 0;
        ListNode result = new ListNode();
        ListNode rCopy = result;
        while (true) {
            if (l1 == null && l2 == null && jinwei == 0){
                break;
            }
            int o;
            int t;
            if (l1 == null) {
                o = 0;
            } else {
                o = l1.val;
            }
            if (l2 == null) {
                t = 0;
            } else {
                t = l2.val;
            }
            int h = o + t;
            if (jinwei != 0) {
                h += jinwei;
                jinwei = 0;
            }
            int s = h / 10;
            int g = h % 10;
            if(s > 0){
                jinwei = s;
            }
            result.next = new ListNode(g);
            result = result.next;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        return rCopy.next;
    }
}
